﻿using CraftParameterConfig.Domain.Entity;
using CraftParameterConfig.Domain.Interfaces;
using CraftParameterConfig.Infrastructure.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CraftParameterConfig.Infrastructure.Data
{
    public class ParameterAccessRespository : RespositoryBase, IParameterAccessRespository
    {
        DbContext dbContext = GlobalObjects.DbContext;
        ObjectChange obc = GlobalObjects.ObjectChange;
        public ParameterAccessRespository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        { }
        public void Add(ParameterAccess entity)
        {
            string sql = GetInsertSql(entity);
            using (DbTransaction dbTransaction = _unitOfWork.BeginTransaction())
            {
                var com = dbContext.GetDbCommand(dbTransaction.Connection);
                dbContext.ExecuteSqlNoQuery(com, sql);
                _unitOfWork.SaveChange();
            }
        }

        public void AddRange(List<ParameterAccess> entitys)
        {
            List<string> sql = new List<string>();
            foreach (var item in entitys)
            {
                sql.Add(GetInsertSql(item));
            }
            using (DbTransaction dbTransaction = _unitOfWork.BeginTransaction())
            {
                var com = dbContext.GetDbCommand(dbTransaction.Connection);
                dbContext.ExecuteSqlNoQuery(com, sql);
                _unitOfWork.SaveChange();
            }
        }

        public void Delete(Guid id)
        {
            string sql = string.Format("Update MDA_PARAMETER_ACCESS set STATUS=99 where GUIDID = '{0}'", id.ToString());
            using (DbTransaction dbTransaction = _unitOfWork.BeginTransaction())
            {
                var com = dbContext.GetDbCommand(dbTransaction.Connection);
                dbContext.ExecuteSqlNoQuery(com, sql);
                _unitOfWork.SaveChange();
            }
        }

        public void Delete(ParameterAccess entity)
        {
            string sql = string.Format("Update MDA_PARAMETER_ACCESS set STATUS=99  where GUIDID = '{0}'", entity.GuidID.ToString());
            using (DbTransaction dbTransaction = _unitOfWork.BeginTransaction())
            {
                var com = dbContext.GetDbCommand(dbTransaction.Connection);
                dbContext.ExecuteSqlNoQuery(com, sql);
                _unitOfWork.SaveChange();
            }
        }

        public void DeleteAll()
        {
            throw new Exception("对表MDA_PARAMETER_ACCESS执行此操作非常危险,因此我们不支持此操作,请单独申请相关操作,并告知相关部门领导人员!");
        }

        public ParameterAccess GetEntityByContition(string condition)
        {
            var result = GetEntitysByContition(condition);
            if (result.Count != 1)
            {
                throw new Exception("当前条件在MDA_PARAMETER_ACCESS表中不能找到唯一一条记录,请重新设置条件再进行查询!");
            }
            return result.FirstOrDefault();
        }

        public ParameterAccess GetEntityByGuid(Guid id)
        {
            string condition = string.Format("where GUIDID='{0}'", id.ToString()); 
            DataTable dtSource = GetTable(condition);
            if (dtSource.Rows.Count != 1)
            {
                throw new Exception("当前条件在MDA_PARAMETER_ACCESS表中不能找到唯一一条记录,请重新设置条件再进行查询!");
            }
            return obc.ChangeTableToObject<ParameterAccess>(dtSource).FirstOrDefault();
        }

        public List<ParameterAccess> GetEntitysByContition(string condition)
        {
            DataTable dtSource = GetTable(condition);
            return obc.ChangeTableToObject<ParameterAccess>(dtSource).ToList();
        }

        public DataTable GetTable(string condition)
        {
            string sql = string.Format("select * from MDA_PARAMETER_ACCESS {0}", condition);
            var con = dbContext.GetDbConnection();
            try
            {
                con.Open();
                return dbContext.GetTable(dbContext.GetDbCommand(con), sql);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }

        public void Update(ParameterAccess entity)
        {
            string sql = string.Format("Update MDA_PARAMETER_ACCESS set STATUS={0},MODIFY_USER='SC',MODIFY_TIME=sysdate  where GUIDID='{1}'", entity.Status, entity.GuidID.ToString());
            using (DbTransaction dbTransaction = _unitOfWork.BeginTransaction())
            {
                var com = dbContext.GetDbCommand(dbTransaction.Connection);
                dbContext.ExecuteSqlNoQuery(com, sql);
                _unitOfWork.SaveChange();
            }
        }

        public void UpdateRange(List<ParameterAccess> entitys)
        {
            List<string> sqls = new List<string>();
            foreach (var item in entitys)
            {
                string sql = string.Format("Update MDA_PARAMETER_ACCESS set STATUS={0},MODIFY_USER='SC',MODIFY_TIME=sysdate where GUIDID='{1}'", item.Status, item.GuidID.ToString());
                sqls.Add(sql);
            }
            using (DbTransaction dbTransaction = _unitOfWork.BeginTransaction())
            {
                var com = dbContext.GetDbCommand(dbTransaction.Connection);
                dbContext.ExecuteSqlNoQuery(com, sqls);
                _unitOfWork.SaveChange();
            }
        }

        private string GetInsertSql(ParameterAccess entity)
        {
            return string.Format("Insert into MDA_PARAMETER_ACCESS(MAINID,GUIDID,VALIDITY,ORDERBATCH,FILETYPE,CREATE_USER)values('{0}','{1}','{2}','{3}',{4},'SC')",
                entity.MainID, entity.GuidID, entity.Validity.ToString("yyyy/MM/dd HH:mm:ss"), entity.OrderBatch, (int)entity.FileType);
        }
    }
}
